package cn.ruicz.demo.ui.login;

import android.app.Application;
import androidx.lifecycle.MutableLiveData;
import androidx.databinding.ObservableBoolean;
import androidx.annotation.NonNull;

import android.view.View;

import cn.ruicz.demo.ui.main.DemoActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import cn.ruicz.basecore.base.BaseViewModel;
import cn.ruicz.basecore.binding.command.BindingAction;
import cn.ruicz.basecore.binding.command.BindingCommand;
import cn.ruicz.basecore.binding.command.BindingConsumer;

import static com.uber.autodispose.AutoDispose.autoDisposable;

/**
 * Created by goldze on 2017/7/17.
 */

public class LoginViewModel extends BaseViewModel {
    //用户名的绑定
    public MutableLiveData<String> userName = new MutableLiveData<>();
    //密码的绑定
    public MutableLiveData<String> password = new MutableLiveData<>();
    public MutableLiveData<NamePwd> namePwd = new MutableLiveData<>();
    //用户名清除按钮的显示隐藏绑定
    public MutableLiveData<Integer> clearBtnVisibility = new MutableLiveData<>();
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable extends UIChangeLiveData{
        //密码开关观察者
        public ObservableBoolean pSwitchObservable = new ObservableBoolean(false);
    }

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.setValue("");
        }
    });
    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchObservable.set(!uc.pSwitchObservable.get());
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                clearBtnVisibility.setValue(View.VISIBLE);
            } else {
                clearBtnVisibility.setValue(View.INVISIBLE);
            }
        }
    });
    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    /**
     * 网络模拟一个登陆操作
     **/
    private void login() {
//        if (TextUtils.isEmpty(userName.getValue())) {
//            ToastUtils.showShort("请输入账号！");
//            return;
//        }
//        if (TextUtils.isEmpty(password.getValue())) {
//            ToastUtils.showShort("请输入密码！");
//            return;
//        }
        //RaJava模拟一个延迟操作
        Observable.empty()
                .delay(1, TimeUnit.SECONDS) //延迟3秒
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> showDialog("正在登陆..."))
                .doOnComplete(() -> {
                    dismissDialog();
                    //进入DemoActivity页面
                    startActivity(DemoActivity.class);
                    //关闭页面
                    finish();
                }).subscribe();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
